package com.godme.homework;


public class DeadLock extends Thread{

    private final static Object lock1 = new Object();
    private final static Object lock2 = new Object();
    private final boolean asc;

    DeadLock(boolean asc, String name){
        super(name);
        this.asc = asc;
    }

    @Override
    public void run() {
        System.err.println(getName() + " : wait for lock" + (asc ? 1: 2));
        synchronized (asc ? lock1 : lock2){
            try{
                Thread.sleep(500);
            }catch (Exception e){
                e.printStackTrace();
            }
            System.err.println(getName() + " : wait for lock" + (asc ? 2 : 1));
            synchronized (asc ? lock2 : lock1){
                System.err.println(Thread.currentThread().getName() + " : " + (asc? "asc":"desc"));
            }
        }
    }

    public static void main(String[] args) {
       new DeadLock(true, "thread-lock1").start();
       new DeadLock(false, "thread-lock2").start();
    }

}
